package cn.wolfcode.web.controller;

import cn.wolfcode.service.ICustomerReportService;
import cn.wolfcode.util.CustomerReportQueryObject;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//报表控制器
@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {

    @Autowired
    private ICustomerReportService customerReportService;
    //报表页面
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo")CustomerReportQueryObject qo){
        PageInfo pageResult = customerReportService.selectCustomerReport(qo);
        model.addAttribute("pageResult",pageResult);
        return "customerReport/list";
    }

    @RequestMapping("/bar")
    public String bar(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据,不分页
        List<HashMap> list = customerReportService.listAll(qo);
        //存储x轴的数据集合
        ArrayList xList = new ArrayList();
        //存储y轴的数据集合
        ArrayList yList = new ArrayList();
        //把数据转换为echart需要结构(数组)
        for (HashMap map : list) {
            xList.add(map.get("groupType"));
            yList.add(map.get("number"));
        }
        //共享到页面(freemarker不能直接显示集合和时间)
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        return "customerReport/bar";
    }

    @RequestMapping("/pie")
    public String pie(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        //获取所有数据(不分页)
        List<HashMap> list = customerReportService.listAll(qo);
        //存储分组类型的数据
        ArrayList groupTypeValues = new ArrayList();
        //存储饼图的数据
        ArrayList data = new ArrayList();
        //把数据转换为echart需要结构  legend要数组    series要键值对数组
        for (HashMap map : list) {
            groupTypeValues.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("number"));
            data.add(temp);
        }
        //共享到页面
        model.addAttribute("groupTypeValues", JSON.toJSONString(groupTypeValues));
        model.addAttribute("data",JSON.toJSONString(data));
        return "customerReport/pie";
    }
}